﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using Sharp.DataTransferObject;
using Sharp.Domain.PlatformBoundedContext.ConfigModule;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Crosscutting;
using Sharp.Infrastructure.Search;

namespace Sharp.Application.Platform
{
    /// <summary>
    /// 系统支持服务
    /// </summary>
    public partial class PlatformServiceImpl
    {

        #region OAuth配置相关
        #region 保存OAuth配置
        /// <inheritdoc />
        /// <summary>
        /// 保存OAuth配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Logging("保存OAuth配置“{@p0.Name}”")]
        public Result SaveOAuthConfig(OAuthConfigDTO model)
        {
            Result res = new Result();
            model.Name = model.Interface.GetDescription();
            var info = model.To<OAuthConfig>();
            if (_oAuthConfigRepository.Exists(x => x.Interface == info.Interface))
            {
                var config = _oAuthConfigRepository.Get(x => x.Interface == info.Interface);
                config.AppId = model.AppId;
                config.AppSecret = model.AppSecret;
                config.Interface = info.Interface;
                config.IsEnable = model.IsEnable;
                config.ReturnUrl = model.ReturnUrl;

                _oAuthConfigRepository.Update(config);
            }
            else
            {
                _oAuthConfigRepository.Add(info);
            }
            Context.Commit();
            return res;
        }
        #endregion

        #region 删除OAuth配置
        /// <inheritdoc />
        /// <summary>
        /// 删除OAuth配置
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Logging("删除OAuth配置“{@p0}”")]
        public Result DeleteOAuthConfig(Guid[] ids)
        {
            var res = new Result();
            if (ids == null)
            {
                res.Err = "Id参数不可为空！";
                return res;
            }

            _oAuthConfigRepository.Remove(ids);
            _oAuthConfigRepository.Context.Commit();
            return res;
        }
        #endregion

        #region 获取一条OAuth配置
        /// <inheritdoc />
        /// <summary>
        /// 获取一条OAuth配置
        /// </summary>
        /// <returns></returns>
        public OAuthConfigDTO GetOAuthConfigInfo(Guid id)
        {
            return _oAuthConfigRepository.Get(id).To<OAuthConfigDTO>();
        }
        #endregion

        #region 获取分页数据

        /// <inheritdoc />
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pf"></param>
        /// <returns></returns>
        public PagedResult<OAuthConfigDTO> GetOAuthConfigPaged(PagingFeature pf)
        {
            var paged = _oAuthConfigRepository.Pagging(pf);

            var output = paged.To<OAuthConfig, OAuthConfigDTO>();


            return output;
        }
        #endregion

        #region 获取所有OAuth配置数据
        /// <inheritdoc />
        /// <summary>
        /// 获取所有OAuth配置数据
        /// </summary>
        /// <returns></returns>
        [Cache]
        public IList<OAuthConfigDTO> GetOAuthConfigs()
        {
            var data = _oAuthConfigRepository.GetList().To<List<OAuthConfigDTO>>();
            return data;
        }

        /// <inheritdoc />
        /// <summary>
        /// 通过ClientID获取OAuth配置
        /// </summary>
        /// <param name="clientId"></param>
        /// <returns></returns>
        public OAuthConfigDTO GetOAuthConfigByClientId(string clientId)
        {
            return _oAuthConfigRepository.Get(x => x.AppId == clientId).To<OAuthConfigDTO>();
        }

        #endregion
        #endregion

    }
}